﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Diagnostics;
using MOJsdk;
using System.Collections.ObjectModel;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_MojBasicFileDetails.xaml
    /// </summary>
    public partial class Control_MojBasicFileDetails : UserControl
    {
        private ObservableCollection<ISO639_1> _langs;

        public Control_MojBasicFileDetails()
        {
            InitializeComponent();
            INI();
        }

        private void INI()
        {
            INIEvents();
            INIControls();
        }
        private void INIEvents()
        {
            _btn_preview.Click += delegate { Process.Start(_txtBox_path.Text); };
            _cmb_language.SelectionChanged += SelectionChanged;
            _cmb_index.SelectionChanged += SelectionChanged;
        }
        private void INIControls()
        {
            _btn_preview.ApplyStyle("0101040");
            _cmb_index.ApplyStyle("0101042");
            _cmb_language.ApplyStyle("0101043");
            _Grid.ApplyStyle("0101044");
            _image.ApplyStyle("0101045");
            _lbl_index.ApplyStyle("0101046");
            _lbl_language.ApplyStyle("0101047");
            _txtBox_path.ApplyStyle("0101048");

            _cmb_index.ApplyTemplate("0201005");
            _cmb_language.ApplyTemplate("0201006");

            _lbl_index.Content = MOJLocalization.Localize("Index") + ": ";
            _lbl_language.Content = MOJLocalization.Localize("Language") + ": ";

            for (int i = 1; i <= 100; i++)
                _cmb_index.Items.Add(i);
        }

        private void LoadFile()
        {
            GlobalVars.TheHost.AddLogEntry("[MOJBasicFileDetails] Starting LoadFile process");
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(FileId);
            if (file == null) return;

            _txtBox_path.Text = file.FileFullPath;

            switch ((MOJFileTypes)file.FileType)
            {
                case MOJFileTypes.ImageFile:
                    _image.Source = new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJPosterIcon_20x20")));

                    _lbl_language.IsEnabled = false;
                    _cmb_language.IsEnabled = false;

                    _cmb_index.IsEnabled = false;
                    _lbl_index.IsEnabled = false;
                    break;

                case MOJFileTypes.MovieFile:
                    _image.Source = new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJMovieIcon_20x20")));
                    _cmb_index.SelectedIndex = file.FileIndex - 1;

                    _lbl_index.IsEnabled = true;
                    
                    _lbl_language.IsEnabled = false;
                    _cmb_language.IsEnabled = false;
                    break;

                case MOJFileTypes.SubtitleFile:
                    _langs = new ObservableCollection<ISO639_1>(GlobalVars.TheHost.LoadLanguages());
                    _cmb_language.ItemsSource = _langs;

                    _image.Source = new BitmapImage(new Uri(GlobalVars.TheHost.SystemVars.GetVar("MoJSubtitleIcon_20x20")));

                    _cmb_index.SelectedIndex = file.FileIndex - 1;
                    _lbl_index.IsEnabled = true;

                    _cmb_language.SelectedIndex = file.ISO639_Code-1;
                    _lbl_language.IsEnabled = true;
                    break;
            }
            GlobalVars.TheHost.AddLogEntry("[MOJBasicFileDetails] LoadFile process completed");
        }

        public int FileId
        {
            get
            {
                return (int)GetValue(FileIdProperty);
            }
            set
            {
                SetValue(FileIdProperty, value);
            }
        }
        public static DependencyProperty FileIdProperty = DependencyProperty.Register("FileId", typeof(int), typeof(Control_MojBasicFileDetails));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property != FileIdProperty)
                return;

            LoadFile();
        }
        

        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MOJBasicFileClass file = GlobalVars.TheHost.GetFile(FileId);

            int xL = _cmb_language.SelectedIndex + 1;
            int xI = _cmb_index.SelectedIndex + 1;

            if (file.FileType == 2)
                return;
            if (file.FileType == 1 && file.ISO639_Code == xL && file.FileIndex == xI)
                return;
            if (file.FileType == 0 && file.FileIndex == xI)
                return;

            file.ISO639_Code = xL;
            file.FileIndex = xI;

            using (SQLite.SQLiteConnection _cn = new SQLite.SQLiteConnection(GlobalVars.TheHost.SystemVars.DataBaseLocal))
            {
                _cn.Update(file);
            }
        }
    }
}
